package per.zhy.demo.apple;

import org.apache.http.client.fluent.Content;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zhy on 2018/4/12.
 */
public class MailReader {

    public static Vector<String> list = new Vector();

    @Test
    public void readAppleMail() throws IOException {
        System.out.println(getCode(getMailId("zhy")));
    }

    public static String getMailId(String name) throws IOException {
        String url = "http://www.maildu.de/mail/listmail?recipient=" + name;
        Response response = Request.Get(url)
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36")
                .addHeader("Cookie", "__cfduid=d832e4ca1fddccbe5193fb6fb5058b7721523586860; _dispomail_session=K3BmdGVLc29LUHFZWHBtYnJmSERncDNUbWhYdGM1bVBRS01jcGJDOVRDWXZMMytQd0dJK3BCQzEvQUFrT1oxWGdNQ2xtMWdZRVArb0d5bDEveFdONSs1Yy85ZDRZU0oyVnh2bXJaeTZjTWdPK0lJRlpncVpuUmtQWWQ5WnhEL2Y5VDNVaXlIUVZkWkowaVBoR2dHNzNoUnMxc0JySzJoQ2grV0ozWGsxMDdCaGdnVitrTFc1MU4rR3hIQkVnK21DLS1ZcU14VzdtaG4vTExCeWZzNjR1elNBPT0%3D--8d9bd141f5a26149d42e003e72ec491bad157ea3")
                .addHeader("X-CSRF-Token", "VTTNsot3HZy6NVhsGZIhIX+MBaR3BcG5ilQoOHyDDeI=")
                .execute();
        Content content = response.returnContent();

        Document document = Jsoup.parse(content.asString(Charset.forName("utf-8")));
        String id = document.select("tr td:matches(appleid@id.apple.com)").parents().first().select("button.btn-danger").attr("data-id");
        System.out.println(id);
        return id;
    }

    public static String getCode(String mailId) throws IOException {
        String url = "http://www.maildu.de/mail/viewplain?id=" + mailId;
        Response response = Request.Get(url)
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36")
                .addHeader("Cookie", "__cfduid=d832e4ca1fddccbe5193fb6fb5058b7721523586860; _dispomail_session=K3BmdGVLc29LUHFZWHBtYnJmSERncDNUbWhYdGM1bVBRS01jcGJDOVRDWXZMMytQd0dJK3BCQzEvQUFrT1oxWGdNQ2xtMWdZRVArb0d5bDEveFdONSs1Yy85ZDRZU0oyVnh2bXJaeTZjTWdPK0lJRlpncVpuUmtQWWQ5WnhEL2Y5VDNVaXlIUVZkWkowaVBoR2dHNzNoUnMxc0JySzJoQ2grV0ozWGsxMDdCaGdnVitrTFc1MU4rR3hIQkVnK21DLS1ZcU14VzdtaG4vTExCeWZzNjR1elNBPT0%3D--8d9bd141f5a26149d42e003e72ec491bad157ea3")
                .addHeader("X-CSRF-Token", "VTTNsot3HZy6NVhsGZIhIX+MBaR3BcG5ilQoOHyDDeI=")
                .execute();
        Content content = response.returnContent();

        Document document = Jsoup.parse(content.asString(Charset.forName("utf-8")));
        Pattern pattern = Pattern.compile("\\d{6}");
        Matcher matcher = pattern.matcher(document.toString());
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }
}
